package com.cyan.app.workflow.activity.SFC;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.InputType;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;

import com.cyan.app.barcode.CaptureActivity;
import com.cyan.app.workflow.R;
import com.cyan.app.workflow.base.activity.BaseFragmentActivity;
import com.cyan.app.workflow.bean.BaseBean;
import com.cyan.app.workflow.bean.GdlkBean;
import com.cyan.app.workflow.common.HttpConfig;
import com.cyan.app.workflow.util.AppUtils;
import com.cyan.app.workflow.util.network.CommHttpListener;
import com.cyan.app.workflow.util.network.VolleyUtil;
import com.android.volley.VolleyError;
import com.google.gson.Gson;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by DXW on 2016/2/19.
 * 工单入库-新增
 */
public class GdlkNewActivity extends BaseFragmentActivity {
    public static final String TAG = "GdlkNewActivity";
    public static final String MANNO = "com.cyan.app.workflow.activity.SFC.GdlkNewActivity.manno";
    private TextView mTextView;
    private Button mButtonManno;
    private EditText mEditTextManno;
    private EditText mEditTextManqty;
    private Button mButtonSubmit;
    private GdlkBean.GdlkMannoBean mMannoBean;
    private ArrayList<GdlkBean.GdlkMannoBean> mMannoBeans;
    private GdlkMannoInfo mGdlkMannoInfo;
    private CheckBox mCheckBox;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.sfc_gdlk_new);
        mEditTextManno = (EditText)findViewById(R.id.sfc_gdlk_manno);
        initView();

        mEditTextManno.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                mMannoBean = new GdlkBean(GdlkNewActivity.this).new GdlkMannoBean();
                mMannoBean.setManno(s.toString());
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
        mEditTextManqty = (EditText)findViewById(R.id.sfc_gdlk_manqty);
        mEditTextManqty.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if(!s.toString().isEmpty()) {
                    try {
                        mMannoBean.setManqty(Integer.parseInt(s.toString()));
                    }catch (Exception e){
                        AppUtils.showTip(GdlkNewActivity.this, "工单数量格式不正确");
                    }
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
        mButtonManno = (Button) findViewById(R.id.sfc_gdlk_manno_saoma);
        mButtonManno.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivityForResult(new Intent(GdlkNewActivity.this, CaptureActivity.class), 0x0002);
            }
        });
        mButtonSubmit=(Button)findViewById(R.id.sfc_gdlk_submit);
        mButtonSubmit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mMannoBean == null ||mMannoBean.getManno().isEmpty()) {
                    AppUtils.showTip(GdlkNewActivity.this, "工单编号不能为空");
                } else if (mMannoBean.getManqty() == 0) {
                    AppUtils.showTip(GdlkNewActivity.this, "工单数量必须大于0");
                } else if (mMannoBean.getManqty() > mGdlkMannoInfo.getWareqty()) {
                    AppUtils.showTip(GdlkNewActivity.this, "入库数量不能大于完工数量");
                } else if (GdlkBean.get(GdlkNewActivity.this).getManno(mMannoBean.getManno()) != null) {
                    AppUtils.showTip(GdlkNewActivity.this, "该工单已添加");
                } else {
                    setMannoResult(true);
                    if(!mCheckBox.isChecked()) {
                        finish();
                    }else {
                        mEditTextManno.setText("");
                        mEditTextManqty.setText("");
                        startActivityForResult(new Intent(GdlkNewActivity.this, CaptureActivity.class), 0x0002);
                    }
                }
            }
        });
        mCheckBox = (CheckBox)findViewById(R.id.sfc_gdlk_new_cb);
        //startActivityForResult(new Intent(GdlkNewActivity.this, CaptureActivity.class), 0x0002);
    }

    private void  setMannoResult(boolean type){
        if(type) {
            mMannoBeans =GdlkBean.get(this).getMannos();
            mMannoBean.setItnbr(mGdlkMannoInfo.getItnbr());
            mMannoBean.setIsSelected(false);
            mMannoBeans.add(mMannoBean);
            setResult(RESULT_OK);
        }else {
            setResult(RESULT_CANCELED);
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
    }

    private void initView(){
        titleBar();
        //工单禁止键盘输入
        mEditTextManno.setInputType(InputType.TYPE_NULL);
    }

    @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            super.onActivityResult(requestCode, resultCode, data);
            if(requestCode == 0x0002 && resultCode == RESULT_OK) {
                String result = "";
                if(data != null) {
                    result = data.getStringExtra("result");
                    getMannoLzInfo(result);
                }
                mEditTextManno.setText(String.valueOf(result));
            }
    }

    private void titleBar(){
        mTextView =(TextView)findViewById(R.id.top_title_info);
        mTextView.setText("添加入库工单");
        Button back = (Button) findViewById(R.id.top_title_back);
        back.getBackground().setAlpha(255);// 透明度0~255透明度值 ，值越小越透明
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //返回按钮
                finish();
            }
        });
    }
    private void getMannoLzInfo(String manno){
        String result = null;
        try {
            Map<String, String> params = new HashMap<String, String>();
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("Manno", manno);
            params.put("action", "205");
            params.put("params", jsonObject.toString());
            VolleyUtil.sendStringRequestByGet(GdlkNewActivity.this, HttpConfig.HTTP_MAIN_SERVER_URL, TAG, params, null, new CommHttpListener<Object>() {
                @Override
                public void onResponse(String result) {
                    super.onResponse(result);
                    if (result == null) {
                        return;
                    }
                    mGdlkMannoInfo = new Gson().fromJson(result, GdlkMannoInfo.class);
                    if (mGdlkMannoInfo.getCode() == -1) {
                        AppUtils.showTip(GdlkNewActivity.this, mGdlkMannoInfo.getMsg());
                    } else {
                        mEditTextManqty.setText(String.valueOf(mGdlkMannoInfo.getWareqty()));
                    }
                }

                @Override
                public void onCodeError(int code, String msg) {
                    AppUtils.showTip(GdlkNewActivity.this, msg);
                }

                @Override
                public void onVolleyError(VolleyError volleyError) {
                    AppUtils.showTip(GdlkNewActivity.this, R.string.network_error);
                }

                @Override
                public void onException(Exception e) {
                    AppUtils.showTip(GdlkNewActivity.this, R.string.network_error);
                }
            });
        }catch (Exception e){
            e.printStackTrace();
            AppUtils.showTip(GdlkNewActivity.this, "执行失败，请联系IT人员");
        }
    }

    class GdlkMannoInfo extends BaseBean {
        private int Wareqty;
        private String Manno;
        private String Itnbr;

        public String getManno() {
            return Manno;
        }
        public void setManno(String manno) {
            Manno = manno;
        }

        public String getItnbr() {
            return Itnbr;
        }
        public void setItnbr(String itnbr) {
            Itnbr = itnbr;
        }

        public int getWareqty() {
            return Wareqty;
        }
        public void setWareqty(int manqty) {
            Wareqty = manqty;
        }
    }
}
